package agency.refunds;

import java.util.Map;
import java.util.TreeMap;

import agency.Refunds;
import dict.AgencyDict;
import dict.CbDict;
import entities.TBank;
import entities.TCard;
import entities.TCb;
import entities.TTransaction;
import net.sf.json.JSONObject;
import util.HttpUtil;
import util.LogUtil;
import utils.NumberUtil;
import utils.StringUtil;

public class Etpedc  implements Refunds{

	private static String PATH = "https://ncp.etonpay.com/Refund";
	private String REMARK = "standard refund";
	
	public void process(TCb cb, TTransaction transaction, TBank bank, TCard card) {
		Map<String, String> requestMap = new TreeMap<String, String>();
		JSONObject responseMap = new JSONObject(); 
		String res = null;
		// 消费

		requestMap.put("bcNo", cb.getCbRef());
		requestMap.put("msAmount", NumberUtil.transfer(cb.getCbAmount()));
		requestMap.put("cbRemark", REMARK);

		// 日志打印发送数据
		StringBuffer send = new StringBuffer();
		for (Map.Entry<String, String> e : requestMap.entrySet()) {
			send.append(e.getKey() + "=" + e.getValue() + "&");
			// System.out.println(e.getKey()+"==="+e.getValue());
		}
		LogUtil.agency("ETPEDC_REFUNDS", send.toString());

		try {
			res = new HttpUtil().ssl(PATH, requestMap);
		} catch (Exception e) {
			cb.setCbMark("555");
			cb.setCbInfo("edc refund data encrpt exception");
			cb.setCbCode(AgencyDict.ETP_TRANSMISSION_EXCEPTION);
			cb.setCbMessage(AgencyDict.AGENCY.get(AgencyDict.ETP_TRANSMISSION_EXCEPTION));
			return;
		}

		if (StringUtil.isEmpty(res)) {
			cb.setCbMark("555");
			cb.setCbInfo("edc refund data response exception");
			cb.setCbCode(AgencyDict.ETP_TRANSMISSION_EXCEPTION);
			cb.setCbMessage(AgencyDict.AGENCY.get(AgencyDict.ETP_TRANSMISSION_EXCEPTION));
			return;
		}

		try {
			responseMap = JSONObject.fromObject(res); 
		} catch (Exception e) {
			cb.setCbMark("555");
			cb.setCbInfo("edc refund data disembly exception");
			cb.setCbCode(AgencyDict.ETP_TRANSMISSION_EXCEPTION);
			cb.setCbMessage(AgencyDict.AGENCY.get(AgencyDict.ETP_TRANSMISSION_EXCEPTION));
			return;
		}

		// 日志打印返回数据
		LogUtil.agency("ETPEDC_REFUNDS", res);

		if (!responseMap.containsKey("statusCode")) {
			cb.setCbMark("555");
			cb.setCbInfo("edc refund data parameter exception");
			cb.setCbCode(AgencyDict.ETP_TRANSMISSION_EXCEPTION);
			cb.setCbMessage(AgencyDict.AGENCY.get(AgencyDict.ETP_TRANSMISSION_EXCEPTION));
			return;
		}

		if (!responseMap.get("statusCode").equals("success")) {
			cb.setCbMark(responseMap.get("errorCode").toString());
			cb.setCbInfo(responseMap.get("errorCode").toString());
			cb.setCbCode(AgencyDict.ETP_REFUND_FAILURE);
			cb.setCbMessage(AgencyDict.AGENCY.get(AgencyDict.ETP_REFUND_FAILURE));
			return;
		}

		cb.setCbState("1");
		cb.setCbMark(responseMap.get("statusCode").toString());
		cb.setCbInfo(responseMap.get("statusCode").toString());
		cb.setCbCode(CbDict.CB_SUCCESS);
		cb.setCbMessage(CbDict.MESSAGES.get(CbDict.CB_SUCCESS));
		return;
	}

	
	
}
